<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <!-- 使用tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDeptBtn('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 主体区域绘制 树形控件-->
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              :default-expand-all="true"
            >
              <template slot-scope="{ data }">
                <!-- 上面组件的复用 -->
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%;"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDeptBtn(data.id)">添加部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDeptBtn(data.id)">编辑部门</el-dropdown-item>
                            <!-- 当前部门有子部门时隐藏删除 -->
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
          <!-- 弹框组件 -->
          <el-dialog
            :title="isEdit ? '编辑' : '新增'"
            :visible.sync="showDialog"
            width="50%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
          >
            <!-- 表单组件 -->
            <dept-dialog
              v-if="showDialog"
              :pid="pid"
              :is-edit="isEdit"
              :origin-list="originList"
              @update-Dept="updateDept"
              @close="closeDialog"
            />
          </el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getdepartment, delDept } from '@/api/departments'
import { transListToTree } from '@/utils'
import DeptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { DeptDialog },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制弹框
      pid: '',
      isEdit: false, // 是否为编辑状态
      originList: [] // 后台返回的扁平数据
    }
  },
  created() {
    this.getdepartment()
  },
  methods: {
    // 获取组织架构数据
    async getdepartment() {
      const res = await getdepartment()
      // console.log(res)
      this.originList = res.data.depts
      this.treeData = transListToTree(res.data.depts, '')
    },
    // 添加子部门
    addDeptBtn(id) {
      this.isEdit = false
      this.showDialog = true
      this.pid = id
    },
    // 编辑子部门
    editDeptBtn(id) {
      this.isEdit = true
      this.showDialog = true
      this.pid = id
    },
    // 删除部门
    async delDept(id) {
      console.log('111111111')
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('已取消删除')
      const res = await delDept(id)
      this.$message.success(res.message)
      this.getdepartment()
    },
    // 子传父:关闭弹框,刷新数据
    updateDept() {
      this.showDialog = false
      this.getdepartment()
    },
    // 取消关闭
    closeDialog() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
  ::v-deep .el-dialog__body {
      padding: 30px 40px;
  }

}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }
  .departments-tree {
    margin: 12px 0px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>

